package com.hgshop.yudongyang.controller;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.hgshop.yudongyang.domain.Brand;
import com.hgshop.yudongyang.domain.Spu;
import com.hgshop.yudongyang.domain.SpuVo;
import com.hgshop.yudongyang.service.BrandService;
import com.hgshop.yudongyang.service.SpuService;





@RequestMapping("spu")
@Controller
public class SpuController {

	@Reference
	SpuService spuService;
	@Reference
	BrandService brandService;
	
	@Value("${file.path}")
	String path;
	
	@RequestMapping("list")
	public String list(SpuVo vo,Model model,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5")Integer pageSize) {
		PageInfo<Spu> info = spuService.list(pageNum, pageSize,vo);
		model.addAttribute("list", info.getList());
		PageInfo<Brand> brandList = brandService.list(1, Integer.MAX_VALUE);
		model.addAttribute("brands", brandList.getList());
		model.addAttribute("info", info);
		model.addAttribute("vo", vo);
		return "spu/list";
	}
	
	@PostMapping("dels")
	@ResponseBody
	public boolean dels(@RequestParam("ids[]")Integer[] ids) {
		
		try {
			spuService.dels(ids);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	@PostMapping("del")
	@ResponseBody
	public boolean del(@RequestParam("id")Integer id) {
		
		try {
			spuService.del(id);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	@GetMapping("add")
	public String add(Model model) {
		PageInfo<Brand> brandsInfo = brandService.list(1, Integer.MAX_VALUE);

		model.addAttribute("brands", brandsInfo.getList());
		
		return "spu/add";
	}
	@PostMapping("add")
	@ResponseBody
	public boolean add(Spu spu,MultipartFile picFileName) {
		
		if(picFileName != null && picFileName.getSize()>0) {
			String fileName=UUID.randomUUID().toString()+picFileName.getOriginalFilename().substring(picFileName.getOriginalFilename().lastIndexOf("."));
			File file = new File(path, fileName);
			try {
				//System.out.println("图片上传------------");
				picFileName.transferTo(file);
				spu.setSmallPic(fileName);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		try {
			spuService.add(spu);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
	@GetMapping("update")
	public String update(Integer id,Model model) {
		
		PageInfo<Brand> brandsInfo = brandService.list(1, Integer.MAX_VALUE);

		model.addAttribute("brands", brandsInfo.getList());
		Spu spu = spuService.getOne(id);
		/*
		 * System.out.println(spu+"2222222222222222");
		 * System.out.println(id+"--------------");
		 */
		model.addAttribute("spu", spu);
		
		return "spu/update";
		
	}
	@ResponseBody
	@PostMapping("update")
	public boolean update(Spu spu,MultipartFile picFileName) {
		
		if(picFileName != null && picFileName.getSize()>0) {
			String fileName=UUID.randomUUID().toString()+picFileName.getOriginalFilename().substring(picFileName.getOriginalFilename().lastIndexOf("."));
			File file = new File(path, fileName);
			try {
				//System.out.println("图片上传------------");
				picFileName.transferTo(file);
				spu.setSmallPic(fileName);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		try {
			spuService.update(spu);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
}
